﻿// <copyright file="WeirValidator.cs" company="City of Portland, BES-ASM">
// </copyright>
// <summary>WeirValidator class</summary>

namespace BESASM.EMGAATS.BusinessClasses
{
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using FluentValidation;

  /// <summary>
  /// A class for validating Weir instances
  /// </summary>
  public class WeirValidator : AbstractValidator<IWeir>
  {
    /// <summary>
    /// Initializes a new instance of the <see cref="WeirValidator"/> class.
    /// </summary>
    /// <param name="ruleSet">The set of rules to apply.</param>
    public WeirValidator(RuleSet ruleSet)
    {
      // Universal rules

      // RuleSet rules
      switch (ruleSet)
      {
        case BusinessClasses.RuleSet.MasterDataCommit:
          this.WeirLengthGreaterThanZero(ErrorLevel.Error);
          this.WeirCoefficientGreaterThanZero(ErrorLevel.Error);
          break;
        case BusinessClasses.RuleSet.ModelDataCommit:
          this.WeirLengthGreaterThanZero(ErrorLevel.Error);
          this.WeirCoefficientGreaterThanZero(ErrorLevel.Error);
          break;
        case BusinessClasses.RuleSet.ModelDeployment:
          this.WeirLengthGreaterThanZero(ErrorLevel.Error);
          this.WeirCoefficientGreaterThanZero(ErrorLevel.Error);
          break;
        case BusinessClasses.RuleSet.NetworkTracing:
          break;
        case BusinessClasses.RuleSet.PropertyDialog:
          this.WeirLengthGreaterThanZero(ErrorLevel.Error);
          this.WeirCoefficientGreaterThanZero(ErrorLevel.Error);
          break;
      }
    }

    private void WeirLengthGreaterThanZero(ErrorLevel level)
    {
      RuleFor(weir => weir.LengthFt)
        .GreaterThan(0.0)
        .WithState(weir => new ValidationState(weir, level))
        .WithMessage(
          "Weir ({0}-{1}) has a length equal to or less than zero.", 
          weir => weir.USNode,
          weir => weir.DSNode);
    }

    private void WeirCoefficientGreaterThanZero(ErrorLevel level)
    {
      RuleFor(weir => weir.Coefficient)
        .GreaterThan(0.0)
        .WithState(weir => new ValidationState(weir, level))
        .WithMessage(
          "Weir ({0}-{1}) has a length equal to or less than zero.", 
          weir => weir.USNode,
          weir => weir.DSNode);
    }
  }
}
